//
//  HMEmoticonPopView.swift
//  EmoticonKeyBoard
//
//  Created by heima on 16/4/18.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit

class HMEmoticonPopView: UIView {

    @IBOutlet weak var emoticonButton: HMEmoticonButtton!
    
    
    //加载当前视图
    class func loadPopView() -> HMEmoticonPopView {
        let v = NSBundle.mainBundle().loadNibNamed("HMEmoticonPopView", owner: nil, options: nil).last as! HMEmoticonPopView
        return v
    }
    
    
    func show(button btn: HMEmoticonButtton) {
        if btn.em!.isDelete || btn.em!.isEmpty {
            //不添加popView
            //移除popView
            hidden = true
            return
        }
        let lastWindow = UIApplication.sharedApplication().windows.last!
        //设置布局
        self.emoticonButton.em = btn.em
        //添加到cell 上
        if self.superview != lastWindow {
            lastWindow.addSubview(self)
        }
        hidden = false
        //设置frame
        //转换坐标 将 btn 的坐标转换到window
        //        let rect = __.convertRect(_rect(如果是自己在转化的时候就使用bounds,如果是父视图 就使用frame)_, toView: _需要转换到的目标视图_)
        let rect = btn.convertRect(btn.bounds, toView: window)
        center.x = CGRectGetMidX(rect)
        frame.origin.y = CGRectGetMaxY(rect) - frame.height
        
    }
    
    func dimissAfter() {
        let delta = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.25))
        dispatch_after(delta, dispatch_get_main_queue()) { () -> Void in
            self.hidden = true
        }
    }

}
